﻿/*Author：Lary Pan(itolmqj123@gmail.com)
 *Date  ：2016/5/10
 *Tips  ：UI基类
 * */
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEditor.Animations;

public class UIBase : MonoBehaviour 
{
    public Anchor anchor = Anchor.BOTTOM;
    public bool keepDepth = false;
    public bool beMask = false;
    public bool beMaskClose = true;
    public bool beAnim = false;
    //private UIPanel _panel;

    private const string Btn_close = "Btn_Close";
    
    private void OnEnable()
    {
        _Attach();
    }
    
    private void Awake()
    {
        //_panel = CHelper.GetSafeComponent<UIPanel>(gameObject);
        InitUI();
    }

    private void Start()
    { 
        InitData();
    }

    private void OnDestroy()
    {
        DoDestroy();
    }

    private void _Attach()
    {
        GameObject uiRoot = GameObject.FindGameObjectWithTag("UIRoot");
        Transform root = uiRoot.transform;
        if (transform.root == root) return;

        switch (anchor)
        {
            //case Anchor.BottomLeft:
            //    root = root.FindChild("BL");
            //    break;
            //case Anchor.Left:
            //    root = root.FindChild("L");
            //    break;
            //case Anchor.TopLeft:
            //    root = root.FindChild("TL");
            //    break;
            //case Anchor.Top:
            //    root = root.FindChild("T");
            //    break;
            //case Anchor.TopRight:
            //    root = root.FindChild("TR");
            //    break;
            //case Anchor.Right:
            //    root = root.FindChild("R");
            //    break;
            //case Anchor.BottomRight:
            //    root = root.FindChild("BR");
            //    break;
            //case Anchor.Bottom:
            //    root = root.FindChild("B");
            //    break;
            //case Anchor.UI3D:
            //    root = root.FindChild("3D");
            //    break;
            case Anchor.BOTTOM:
                root = root.FindChild("BOTTOM");
                break;
            case Anchor.MID:
                root = root.FindChild("MID");
                break;
            case Anchor.UP:
                root = root.FindChild("UP");
                break;
            default: 
                root = root.FindChild("C");
                break;
        }

        CHelper.Attach(root, transform, true);
    }

    //初始化界面
    protected virtual void InitUI() 
    {
         _SetMaskBg();
         _SetCloseBtn();
         _SetAnim();
    }

    //初始化数据
    protected virtual void InitData() { }

    //消毁
    protected virtual void DoDestroy() { Close(); }


  //  public int PanelDepth
  //  {
		//set 
  //      {  
		//	UIPanel[] panels = transform.GetComponentsInChildren<UIPanel>(true);
		//	foreach(UIPanel temp in panels)
		//	{
		//		temp.depth += value;
		//	}
		//	_panel.depth = value;
  //      }
		//get { return _panel.depth; }
  //  }

    
    public void Close()
    {
        UIRecord record = new UIRecord();
        record.uibase = this;
        UIManager.Instance.RemoveUI(record);
        Destroy(gameObject);
    }


    #region InitUI
    private void _SetCloseBtn()
    {
        Transform node = CHelper.FindNode(transform, Btn_close);
        if (node)
        {
            node.GetComponent<Button>().onClick.AddListener(Close);
        }
    }

    private void _SetMaskBg()
    {
        if (!beMask) return;

        ResHelper.LoadLocalRes(GlobleConfig.ResPath.UIMask, _obj =>
        {
			GameObject obj = _obj as GameObject;
            CHelper.Attach(transform, obj.transform,true,SlibingMode.first);
            if (beMaskClose)
                EventTriggerListener.Get(obj).onClick = (gObj) => { Close(); };
        });
    }

    
    private void _SetAnim()
    {
        if (!beAnim) return;

        ResHelper.LoadLocalRes(GlobleConfig.ResPath.Animator_UIShow,  _animator => {  
            GameObject obj = transform.Find("Obj").gameObject;
            if (obj == null) return;

            AnimatorController animatorController = _animator as AnimatorController;
            Animator animator = CHelper.GetSafeComponent<Animator>(obj);
            animator.runtimeAnimatorController = animatorController;
            animator.SetBool("Open", true);
            //AnimationClip clip = _clip as AnimationClip;
            //Animation anim = CHelper.GetSafeComponent<Animation>(obj);
            //anim.playAutomatically = false;
            //clip.legacy = true;     //警告：在unity5以后，要使用Animation.AddClip()必须加这一句，否则无效
            //anim.AddClip(clip, "UIShow");
            //anim.clip = clip;
            //anim.Play();
        });
    }

    #endregion
    

    #region 点击事件监听
    public void SetBtnClick(GameObject btn, string funcName)
    {
        //CHelper.GetSafeComponent<UIScaleButton>(btn);
        UIButtonProxy btnProxy = CHelper.GetSafeComponent<UIButtonProxy>(btn);
        btnProxy.target = gameObject;
        btnProxy.functionName = funcName;
    }

    //列表点击监听
    public void SetItemClick(GameObject item, int index, string funcName)
    {
        //CHelper.GetSafeComponent<UIScaleButton>(item);
        UIButtonIntProxy btnProxy = CHelper.GetSafeComponent<UIButtonIntProxy>(item);
        btnProxy.target = gameObject;
        btnProxy.data = index;
        btnProxy.functionName = funcName;
    }

    #endregion
}
